<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Lk
 * @Date: 2023-06-28 15:42:23
 * @LastEditors: Lk
 * @LastEditTime: 2023-07-03 16:42:55
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\MoneyLog;
use app\common\model\OpenArea;
use app\common\model\Requirement;
use app\common\model\requirement\Lists;
use app\common\model\requirement\Offer;
use app\common\model\User;
use think\Db;

/**
 * 广场接口
 */
class Plaza extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (添加需求)
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="发布方式")
     * @ApiParams (name="title", type="string", required=true, sample="", description="标题")
     * @ApiParams (name="mobile", type="string", required=true, sample="", description="联系方式")
     * @ApiParams (name="price", type="string", required=true, sample="", description="价格")
     * @ApiParams (name="province", type="string", required=true, sample="", description="省")
     * @ApiParams (name="city", type="string", required=true, sample="", description="市")
     * @ApiParams (name="area", type="string", required=true, sample="", description="区")
     * @ApiParams (name="address", type="string", required=true, sample="", description="地址")
     * @ApiParams (name="lng", type="string", required=true, sample="", description="经度")
     * @ApiParams (name="lat", type="string", required=true, sample="", description="纬度")
     * @ApiParams (name="content", type="string", required=false, sample="", description="需求")
     * @ApiParams (name="images", type="string", required=false, sample="", description="图片")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function add()
    {
        $post = request()->post();
        extract($post);
        $validate = validate('requirement.Lists'); //使用助手函数
        $result = $validate->check($post);
        if (!$result) {
            $this->error($validate->getError());
        }
        $post['user_id'] = $this->auth->id;
        $post['sn'] = sn('requirement', 'sn', 'RE');
        $requirement = new Lists($post);
        $res = $requirement->allowField(true)->save();
        if ($res) {
            $this->success('发布成功');
        }
        $this->error('发布失败');
    }


    /**
     * @Author LK
     * @ApiTitle (需求列表)
     * @ApiMethod (GET)
     * @ApiParams (name="state", type="integer", required=true, sample="", description="发布方式:1=竞价,2=线下沟通")
     * @ApiParams (name="province", type="string", required=true, sample="", description="省")
     * @ApiParams (name="city", type="string", required=true, sample="", description="市")
     * @ApiParams (name="area", type="string", required=true, sample="", description="区")
     * @ApiParams (name="type", type="integer", required=true, sample="", description="1=我的需求，2=广场需求")
     * @ApiParams (name="status", type="string", required=true, sample="", description="状态值:0=待审核,10=已审核,20=竞价中/待支付,30=待接单,40=已接单,50=已完成,-1=审核失败")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $get = request()->get();
        extract($get);
        $user = User::get($this->auth->id);
        $list = new Lists();
        if ($type == 1) {
            $list->where('user_id', $user->id);
        }
        if (isset($state) && $state) {
            $list->where('type', $state);
        }
        if (isset($province) && $province) {
            $list->where('province', $province);
        }
        if (isset($city) && $city) {
            $list->where('city', $city);
        }
        if (isset($area) && $area) {
            $list->where('area', $area);
        }
        $lists = $list->where('status', $status)->order('id desc')->paginate();
        $this->success('请求成功', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (需求详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="需求ID")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $get = request()->get();
        extract($get);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $data = Lists::get($id);
        $this->success('请求成功', $data);
    }

    /**
     * @Author LK
     * @ApiTitle (查看竞价)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="需求ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function viewOffer()
    {
        $get = request()->get();
        extract($get);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $lists = Offer::all(['r_id' => $id], ['user']);
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (开通区域)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function region()
    {
        $get = request()->get();
        $lists = OpenArea::all();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (取消广场订单)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function cancelOrder()
    {
        $post = request()->post();
        extract($post);
        $data = Lists::get($id);
        if ($data->status > '20') {
            $this->error('状态错误');
        };
        $data->deletetime = time();
        $res = $data->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
    /**
     * @Author LK
     * @ApiTitle (接单)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function taking_order()
    {
        $post = request()->post();
        extract($post);
        $data = Lists::get($id);
        if ($data->status != '30') {
            $this->error('状态错误');
        };
        $data->status = '40';
        $data->takingtime = time();
        $res = $data->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
    /**
     * @Author LK
     * @ApiTitle (完成订单)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function take_order()
    {
        $post = request()->post();
        extract($post);
        $data = Lists::get($id);
        if ($data->status != '40') {
            $this->error('状态错误');
        };
        Db::startTrans();
        try {
            $data->status = '50';
            $data->taketime = time();
            $data->save();
            $bidding_commission = config('site.bidding_commission');
            if ($bidding_commission > 0 && $data->pay_money > 0 && $data->type == '1') {
                $commission = bcdiv($data->pay_money, $bidding_commission, 2);
                $money = bcsub($data->pay_money, $commission, 2);
                MoneyLog::income($data->employee_id, $money, 1, 103, $data->sn);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('失败');
        }
        $this->success('成功');
    }
}
